<?php
/*====================
 // URL规则
=========================*/

//==========管理后台URL===========

/**
 * admin_url管理后台URL
 * @param string $c
 * @param string $a
 * @param string|array $params 参数
 * @return string
 */
function aurl($c,$a,$params=array(),$ismenuid=false){
	if(!$ismenuid){
		$params['menuid'] = $_GET['menuid'];
	}
	if(defined('HA_HASH')){
		$params['ha_hash'] = HA_HASH;
	}
	return hurl('admin',$c,$a,$params);
}

/**
 * 打卡相关的url
 * @param unknown_type $c
 * @param unknown_type $a
 * @param unknown_type $params
 * @param unknown_type $anchor
 */
function dkurl($c,$a,$params=array(),$anchor=''){
	if($c=='dk'&&$a=='learn'){
		$c="lesson";
		$a='show';
	}
	if($c=='lesson'){
		if($a=='show'){
			$url="/dk/";
			//学习内容页
			$tab=intval($params['tab']);
			if($params['id']){
				$url.=$params['id'];
				unset($params['id']);
			}
			switch ($tab){
				case 1:
					$url.='-bj';
					break;
				case 2:
					$url.='-qz';
					break;
			}
			unset($params['tab']);
			unset($params['tab']);
			if($params['nid']){
				$url.="-n".$params['nid'];
				unset($params['nid']);
			}
			if($params['bjid']){
				$url.="-c".$params['bjid'];
				unset($params['bjid']);
			}
			if($params['uid']){
				$url.="-u".$params['uid'];
				unset($params['uid']);
			}
			if($params['distid']){
				$url.="-d".$params['distid'];
				unset($params['distid']);
			}
			if($params['position']){
				$url.="-pos".$params['position'];
				unset($params['position']);
			}
			if($params['page']){
				$url.="-p".$params['page'];
				unset($params['page']);
			}
		}
	}	
	elseif($c=="dk"){
		if($a=="index"){
			$url="/dk/";
		}		
	}
	elseif($c=="course"){
		//课程相关
		if($a=="index"){
			$url="/dk/course/";
		}else if($a=="category"){
			if($params['id']){
				$url="/dk/course-list-".$params['id'];
				unset($params['id']);
			}else if($params['tagid']){				
				$url="/dk/course-tag-".$params['tagid'];
				unset($params['tagid']);
			}else if($params['uid']){				
				$url="/dk/course-user-".$params['uid'];
				unset($params['uid']);
			}
		}else if($a=="view"){
			$url="/dk/course-view-".$params['id'];
			unset($params['id']);
		}
	}
	elseif($c=="mydk"){
		//我的打卡
		if(in_array($a, array("record","fav","favview","notes","learn"))){				
			$url='/dk/mydk/'.$a;
		}
	}
	if(!$url){
		$url = '/hrloo.php?m=dk2&c='.$c.'&a='.$a;
	}
	foreach ($params as $k=>$v){
		if(!$v){
			unset($params[$k]);
		}
	}	
	if($params){
		$jg=strpos($url,"?")?"&":"?";
		if(is_array($params)){
			$url .= $jg.http_build_query($params);
		}
	}
	if($anchor)
	{
		$url.='#'.$anchor;
	}
	return 'http://www.hrloo.com'.$url;
}
/**
 * 专题url
 * @param unknown_type $c
 * @param unknown_type $a
 * @param unknown_type $params
 * @param unknown_type $anchor
 */
function specialurl($c,$a,$params,$anchor){
	$Root='http://www.hrloo.com';
	if($c=="dktghd"&&$a=="findniuren"){
		$url="/zt05";
	}else if($c=='ceremony'&&$a=="init"){
		$url="/ceremony";
	}
	else if($c=='bianlun'){
		if($a=='init'){
			$url='/class/bianlun/'.$params['id'];
			unset($params['id']);
			if(!$params['tab']){
				unset($params['tab']);
			}
		}
	}else if($c=='baoming'){
		switch ($a){
			case "sz":
				$url="/baoming/sz";
				break;
			case "xyjj":
				$url="/baoming/sz/xyjj";
				break;
			case "szjs":
				$url="/baoming/sz/szjs";
				break;
			case "bkzn":
				$url="/baoming/sz/bkzn";
				break;
		}
	}else if($c=='fangtan'){
		if($a=='init'){
			$url='/class/fangtan/'.$params['id'];
			unset($params['id']);
		}
	}else if($c=="anniversary"){
		if($a=="index2013"){
			$url="/anniversary2013";
		}else if($a=="qfaward2013"){			
			$url="/anniversary2013/qf";
		}
	}
	else if($c=="special"){
		$url="/".$a;
	}else if($c=="diaocha"){
		$Root="http://zl.hrloo.com";
		if($a=="index"){
			$url.="/diaocha/".$params["id"];
			unset($params["id"]);
		}else if($a=="view"){
			$url.="/diaocha/view/".$params["id"];
			unset($params["id"]);
		}else if($a=="result"){
			$url.="/diaocha/result/".$params["id"];
			unset($params["id"]);
		}
	}else if($c=="qqunion" && $a=="index"){		
		$url.="/qqlm";
	}else if($c=="invite"){
		if($a=='index'){
			$url="/".$c;
		}else if($a=='tuijian'){
			$url="/".$a;
		}
	}
	if(!$url){
		$url = '/hrloo.php?m=special&c='.$c.'&a='.$a;
	}
	if($params){
		$jg=strpos($url,"?")?"&":"?";
		if(is_array($params)){
			$url .= $jg.http_build_query($params);
		}
	}
	if($anchor)
	{
		$url.='#'.$anchor;
	}	
	return $Root.$url;
}
/**
 * 奖励url
 * @param unknown_type $c
 * @param unknown_type $a
 * @param unknown_type $params
 * @param unknown_type $anchor
 */
function jlurl($c,$a,$params=array(),$anchor=''){
	if($a=='init'){
		$url="/jl";
		if($params['tab']==1){
			$url="/jl/zj";
		}
		if($params['year'] && $params['month']){
			$url.='/'.$params['year'].'-'.$params['month'];
		}
		unset($params['tab'],$params['year'],$params['month']);
	}
	elseif($a=='view'){
		$url="/jl/".$params['ym'];
		unset($params);
	}
	if(!$url){
		$url = '/hrloo.php?m=jl&c='.$c.'&a='.$a;
	}
	if($params){
		$jg=strpos($url,"?")?"&":"?";
		if(is_array($params)){
			$url .= $jg.http_build_query($params);
		}
	}
	if($anchor)
	{
		$url.='#'.$anchor;
	}
	return 'http://www.hrloo.com'.$url;
}
/**
 * 总结url重写
 * @param unknown_type $c
 * @param unknown_type $a
 * @param unknown_type $params
 * @param unknown_type $anchor
 */
function rzurl($c,$a,$params=array(),$anchor=''){
	if($c=="myrz"){
		if($a=="init"){
			switch ($params['tab']){
				case 2:
					$url="/myrz/collect";
					break;
				case 3:
					$url="/myrz/drafts";
					break;					
				default:
					$url="/myrz/post";
					break;
			}
			unset($params['tab']);
		}else if($a=="edit"){
			$url="/myrz/edit";
		}		
	}else if($c=="rz"){
		switch ($a){
			case "init":
				$url="/rz/";
				break;
			case "search":
				$url="/rz/search";
				break;
			case "lists":
				$url="/rz/lists/";
				break;
			case "show":
				$url="/rz/".$params["id"].".html";
				unset($params["id"]);
				break;
		}
	}else if($c=="zw"){
		switch ($a){
			case "init":				
				if($params["id"]){
					$url="/rz/zw/".$params["id"].".html";
				}else{					
					$url="/rz/zw/";
				}				
				unset($params["id"]);
				break;
			case "lists":
				$url="/rz/zw/lists";
				break;
		}
	}else if($c=="zz"){
		switch ($a){
			case "init":
				$url="/rz/zz/";
				unset($params["id"]);
				break;
			case "show":
				$url="/rz/zz/".$params["id"];				
				unset($params["id"]);
				if($params['bid']){
					$url.="-".$params["bid"];		
					unset($params["bid"]);
					unset($params["type"]);
				}else if($params["type"]){					
					$url.="-0-".$params["type"];	
					unset($params["type"]);
				}
				$url.=".html";
				break;
		}
	}
	if(!$url){
		$url = '/hrloo.php?m=rz&c='.$c.'&a='.$a;
	}
	if($params){
		$jg=strpos($url,"?")?"&":"?";
		if(is_array($params)){
			$url .= $jg.http_build_query($params);
		}
	}
	if($anchor)
	{
		$url.='#'.$anchor;
	}
	return 'http://www.hrloo.com'.$url;
}
/**
 * 班级url重写
 * @param unknown_type $c
 * @param unknown_type $a
 * @param unknown_type $params
 * @param unknown_type $anchor
 */
function bjurl($c,$a,$params,$anchor,$m='bj'){
	//勤奋班
	if($c=='qinfen'){
		if($a=='init'){
			$url='/class/qinfen';
		}if($a=="rd"){			
			return 'http://www.hrloo.com/class/'.$params["view"];
		}
		else if($a=='view'){
			if(!$params['id']){
				//自己班级
				return 'http://www.hrloo.com/class/qinfen/myclass';			 
			}
			$url='/class/qinfen-'.$params['id'];
			unset($params['id']);
			$tab=intval($params['tab']);
			unset($params['tab']);
			switch ($tab){
				case 0:
					//默认列表页
					if($params['n1']=="3"){
						$url.='/taolun/'.$params['tid'];//讨论详细页
						unset($params['tid']);
						unset($params['n1']);
					}else{
						$url.='/taolun/';
					}
					break;
				case 1:
					$url.='/jiangli/';
					break;
				case 2:
					$url.='/huodong/';
					break;
				case 3:
					$url.='/find/';
					break;
			}						
		}
	}else if($c=='zongjie'){
		if($a=='member'){
			$url='/class/zongjie-'.$params['id'].'/members';
			unset($params['id']);
		}else if($a=='view'){			
			$url='/class/zongjie-'.$params['id'];
			unset($params['id']);
			if($params['tab']==4){
				$url.='/taolun/';
			}
			unset($params['tab']);
		}else if($a=='init'){			
			$url='/class/zongjie/';
		}
	}
	if(!$url){
		$url = '/hrloo.php?m='.$m.'&c='.$c.'&a='.$a;
	}
	if($url)
	{
		if($params)
		{
			$jg=strpos($url,"?")?"&":"?";
			if(is_array($params)){
				$url .= $jg.http_build_query($params);
			}else{
				$url .= $jg.$params;
			}
		}
	}
	if($anchor)
	{
		$url.='#'.$anchor;
	}
	return 'http://www.hrloo.com'.$url;
}
function ucurl($c,$a,$params,$anchor){
	if($c=='index'){
		if($a=='init'){
			$url='/myhrloo';
		}
	}else if($c=='friend'){
		if($a=='request'){
			$url='/myhrloo/friend/request';
		}else if($a=='find'){
			$url='/myhrloo/friend/search';
		}else if($a=='my'){			
			$url="/myhrloo/friend";
		}
	}
	else if($c=="sz")
	{
		if(in_array($a, array("accountbind","accountset")))
		{
			$url="/setting/";
			switch ($a)
			{
				case "accountset":
					$url.='1';
					break;
				case "accountbind":
					$url.='2';
					break;
			}
		}
	}
	else if($c=="xx")
	{
		if($a=="getxxlist"){
			$url="/message/1";
		}else if($a=='sendxx'){
			$url.='/message/send';
		}
	}
	else if($c=='cj'&&$a=="init")
	{
		$url='/myhrloo/score/';
	}
	else if($c=="xj")
	{
		if($a=="init"){
			$url="/myhrloo/profile/";
		}else if($a=='edit'){			
			$url="/myhrloo/profile/edit";
		}
	}
	else if($c=="dj"&&$a=="rank")
	{
		$url="/myhrloo/level/1";
	}
	else if($c=="dj"&&$a=="score")
	{
		$url="/myhrloo/level/2";
	}
	else if($c=="personal")
	{
		$urlinfo="";
		if(in_array($a, array("init","daka","zlshare","friend","blog")))
		{
			$url="/home/";
			if($params['uid'])
			{
				$url.=$params['uid'].'-1';
			}else{
				$url.='1';//兼容以前
			}
			unset($params['uid']);
		}
	}
	if(!$url){
		$url = '/hrloo.php?m=uc&c='.$c.'&a='.$a;
	}
	if($url)
	{
		if($params)
		{		
			$jg=strpos($url,"?")?"&":"?";
			if(is_array($params)){
				$url .= $jg.http_build_query($params);
			}else{
				$url .= $jg.$params;
			}
		}
	}	
	if($anchor)
	{
		$url.='#'.$anchor;
	}
	return 'http://www.hrloo.com'.$url;
}
/**
 * 小组url
 * 
 * @param $c unknown_type       	
 * @param $a unknown_type       	
 * @param $params unknown_type       	
 * @param $anchor unknown_type       	
 * @return string
 */
function hgroupurl($c, $a, $params, $anchor) {
	$urlbase = 'http://www.hrloo.com/group';
	if ($c == 'exam') {
		if ($a == 'init') {
			$tab = $params ['tab'];
			unset ( $params ['tab'] );
			if (in_array ( $tab, array (0, 1, 2, 3 ) )) {
				$url = $urlbase . '/exam';
				switch ($tab) {
					case 0 :
						if ($params ['view']) {
							$url = $url . '/taolun/' . $params ['tid'];
							unset ( $params ['view'] );
							unset ( $params ['tid'] );
						}
						break;
					case 1 :
						$url = $url . '/files';
						break;
					case 2 :
						$url = $url . '/test';
						break;
					case 3 :
						$url = $url . '/info';
						break;
				}
			}
		}
	} else if ($c == 'read') {
		if ($a == 'init') {
			$tab = $params ['tab'];
			unset ( $params ['tab'] );
			if (in_array ( $tab, array (0, 1, 2, 3 ) )) {
				$url = $urlbase . '/read';
				switch ($tab) {
					case 0 :
						$url .= '/recommend';
						break;
					case 1 :
						if ($params ['view']) {
							$url .= '/taolun/' . $params ['tid'];
							unset ( $params ['view'] );
							unset ( $params ['tid'] );
						}
						break;
					case 2 :
						$tp = $params ['tp'];
						unset ( $params ['tp'] );
						if (in_array ( $tp, array (0, 1, 2 ) )) {
							switch ($tp) {
								case 0 :
									$url .= '/myread/recommend';
									break;
								case 1 :
									$url .= '/myread/review';
									break;
								case 2 :
									$url .= '/myread/collect';
									break;
							}
						}
						break;
					case 3 :
						$url .= '/myread/form';
						break;
				}
			}
		}
	} else if ($c == 'study') {
		if ($a == 'init') {
			$tab = $params ['tab'];
			unset ( $params ['tab'] );
			if (in_array ( $tab, array (0, 1 ) )) {
				$url = $urlbase . '/study';
				switch ($tab) {
					case 0 :
						if ($params ['view']) {
							$url .= '/taolun/' . $params ['tid'];
							unset ( $params ['view'] );
							unset ( $params ['tid'] );
						}
						break;
					case 1 :
						$url .= '/reward';
						break;
				}
			}
		}
	} else if ($c == 'anli') {
		if ($a == 'init') {
			$tab = $params ['tab'];
			unset ( $params ['tab'] );
			if (in_array ( $tab, array (0, 1 ) )) {
				$url = $urlbase . '/anli';
				switch ($tab) {
					case 0 :
						if ($params ['view']) {
							$url .= '/taolun/' . $params ['tid'];
							unset ( $params ['view'] );
							unset ( $params ['tid'] );
						}
						break;
					case 1 :
						if ($params ['view']) {
							$url .= '/show/' . $params ['id'];
							unset ( $params ['view'] );
							unset ( $params ['id'] );
						}else{
							$url .= '/list';
						}
						break;
				}
			}
		}
	} else if ($c == 'share') {
		if ($a == 'init') {
			$url = $urlbase . '/share';
		}else if( $a == 'view'){
			$tab = intval($params ['tab']);
			unset ( $params ['tab'] );
			if (in_array ( $tab, array (0, 1 ) )) {
				$url = $urlbase . '/share';
				switch ($tab) {
					case 0 :
						$url .= '/my';
						break;
					case 1 :
						$url .= '/my/' . $tab;
						break;
				}
			}
		}
	}

	if (! $url) {
		$url = 'http://www.hrloo.com/hrloo.php?m=group&c=' . $c . '&a=' . $a;
	}
	if ($params) {
		$jg = strpos ( $url, "?" ) ? "&" : "?";
		if (is_array ( $params )) {
			$url .= $jg . http_build_query ( $params );
		}
	}
	if ($anchor) {
		$url .= '#' . $anchor;
	}
	
	return $url;
}
/**
 * Hrloo_url新版URL的入口(不带跳转的)
 * @param string $m
 * @param string $c
 * @param string $a
 * @param string|array $params
 * @param string $anchor  锚点
 * @return string
 */
function hurl($m,$c,$a,$params=array(),$anchor=''){	
	if($m=='jl'){
		return jlurl($c,$a,$params,$anchor);
	}
	if($m=='bj'||$m=="bj2"){
		return bjurl($c, $a, $params, $anchor,$m);
	}
	if($m=='uc')
	{
		return ucurl($c, $a, $params, $anchor);
	}
	if($m=="dk2"){
		return dkurl($c, $a,$params,$anchor);
	}
	if($m=="special"){
		return specialurl($c,$a,$params,$anchor);
	}
	if($m=="rz"){
		return rzurl($c, $a,$params,$anchor);
	}
	if($m=="group"){
		return hgroupurl($c, $a,$params,$anchor);
	}
	if($m=="home"&&$c=="index"&&$a=="guide"){
		return "http://www.hrloo.com/home/guide";
	}
	if($m=="zl"){
		if($c=="zl"){
			if($a=="collect"){
				//资料征集页面
				$url='/';
			}elseif ($a=="reward"){
				//奖励详情
				$url='/reward/'.$params['zjid'];
				unset($params['zjid']);				
			}elseif($a=="show"){
				//资料详情页
				$url="/file/".$params['id'];
				if($params['taskid']){
					$url="/file/".$params['id']."/".$params['taskid'];
				}
				unset($params['id']);	
				unset($params['taskid']);
			}elseif($a=="rule"){
				//资料下载规则				
				$url="/rule/";
			}else if($a=="free"){				
				$url="/free/";
			}
		}elseif($c=="special"){
			if($a=="init"){
				if($params['catid']){					
					$url='/special/sort_'.$params['catid'];
					unset($params['catid']);
				}else{					
					//专题资料首页
					$url='/special/';
				}
			}elseif($a=="categorys"){
				//专题分类列表页				
				$url='/special/sort_'.$params['catid'];
				unset($params['catid']);
			}elseif($a=="show"){				
				//专题内容显示页
				$url='/special/'.$params['id'];
				unset($params['id']);
			}
		}elseif($c=="zlupload"){
			if($a=="upload"){
				$url='/uploadzl/';
			}
		}elseif($c=="myzl"){
			if($a=="myupload"){	
				//我的上传			
				$url='/upload/';
			}elseif($a=="mydownload"){	
				//我的下载		
				$url='/download/';
			}elseif($a=="mycollect"){
				//我的收藏				
				$url='/collect/';
			}
		}
		if(!$url){
			$url = '/hrloo.php?m='.$m.'&c='.$c.'&a='.$a;
		}		
		if($params){
			$jg=strpos($url,"?")?"&":"?";
			if(is_array($params)){
				$url .= $jg.http_build_query($params);
			}else{
				$url .= $jg.$params;
			}
		}
		if($anchor)
		{
			$url.='#'.$anchor;
		}		
		return "http://zl.hrloo.com".$url; 
	}
	elseif($m=="passport")//TODO:后面再去掉注释
	{
		if($c=="index")
		{
			$url="https://passport.hrloo.com/user/".$a;
		}
		else if($c=="bind")
		{
			switch ($a)
			{
				case "qqlogin":					
					$url="https://passport.hrloo.com/bind/login/qq/qzone/";
					break;
				case "sinalogin":
					$url="https://passport.hrloo.com/bind/login/sina/weibo/";
					break;
				case "bindaccount":
					$url="https://passport.hrloo.com/bind/bindaccount/";
					break;
				case "qqcallback":
					$url="https://passport.hrloo.com/bind/callback/qq/qzone/";
					break;
				case "weibocallback":
					$url="https://passport.hrloo.com/bind/callback/sina/weibo/";
					break;
			}
		}
		if($url)
		{
			if($params)
			{
				if(is_array($params)){
					$url .= '?'.http_build_query($params);
				}else{
					$url .= '?'.$params;
				}
			}
			return $url;
		}
	}
	elseif($m=='about')
	{
		if($c=='index')
		{
			switch ($a)
			{
				case 'notice':
					return 'http://www.hrloo.com/about/notice.html';
					break;
				case 'friendlink':
					return 'http://www.hrloo.com/about/friendlink.html';
					break;
				case 'contact':
					return 'http://www.hrloo.com/about/contact.html';
					break;
			}
		}
	}
	if(!$url) //通用规则
	{		
		$url = $url_root.'/hrloo.php?m='.$m.'&c='.$c.'&a='.$a;
		if($params){
			if(is_array($params)){
				$url .= '&'.http_build_query($params);
			}else{
				$url .= '&'.$params;
			}
		}
	}	
	if($anchor)
	{
		$url.='#'.$anchor;
	}
	return 'http://www.hrloo.com'.$url;
}
/**
 * 返回用户个人首页地址
 * @param int $uid
 * @param string $tab  为空时表示个人主页,blog,daka,zl_share,friend
 * @return string
 */
function hpersonalurl($uid,$action='')
{
	if(!$action){
		$action = 'init';
	}
	return hurl('uc','personal',$action,array('uid'=>$uid));
}
/**
 * 返回用户中心URL
 */
function hucurl()
{
	return hurl('uc','index','init');
}
/*
 * 未登录跳转
 */
function hlogin_url($referer='')
{
	if(!$referer)
	{
		return hurl('passport','index', 'login',array('referer'=>hself_url(true)));
	}
	else
	{		
		return hurl('passport','index', 'login',array('referer'=>urlencode($referer)));
	}
}
/**
 * 注册地址
 * @return string
 */
function hregister_url($referer='')
{
	if($referer){		
		return hurl('passport','index', 'register',array('referer'=>urlencode($referer)));
	}else{		
		return hurl('passport','index', 'register',array('referer'=>hself_url(true)));
	}
}
function hself_url($isencode)
{
	$path=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
	if($path=='www.hrloo.com/hrloo.php'||$path=='zl.hrloo.com/hrloo.php')
	{
		$m=$_GET['m'];
		$c=$_GET['c'];
		$a=$_GET['a'];
		$param=array();
		foreach($_GET as $k=>$v)
		{
			if(!in_array($k, array('m','c','a')))
			{
				$param[$k]=$v;
			}
		}
		if($m=="home"&&$c=="index"&&$a=="init"){
			$self_url= "http://www.hrloo.com";
		}else{
			$self_url=hurl($m, $c, $a,$param);
		}
		if($isencode){
			$self_url=urlencode($self_url);
		}
		return $self_url;
	}
	else
	{	
		if($path=='www.hrloo.com/index.php'||$path=='zl.hrloo.com/index.php'){
			$m=$_GET['m'];
			$c=$_GET['c'];
			$a=$_GET['a'];
			if(!$a){
				$a="init";
			}
			if($m=="home"&&$c=="index"&&$a=="init"){
				$self_url= "http://www.hrloo.com";
			}
			else if($m=="zl"&&$c=="zl"&&$a=="init"){				
				$self_url= "http://zl.hrloo.com";
			}			
		}
		if(!$self_url){			
			$self_url= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
		}
		if($isencode){
			$self_url=urlencode($self_url);
		}
		return $self_url;		
	}
}

/**
 * 打卡频道首页
 */
function h_dk_index_url(){
	return hurl('dk2','dk','index');
}
/**
 * 用户中心首页
 */
function h_uc_index_url(){	
	return hurl('uc','index','init');
}
/**
 * 记录广告点击数链接
 * @param unknown_type $id 广告id
 * @param unknown_type $referer 广告链接
 * @param unknown_type $type 类型 1：广告，2：推荐位广告
 */
function hhit($id,$referer,$type){
	return hurl("global","api","hit",array("referer"=>$referer,"id"=>$id,'type'=>$type));
}

